//const fs = require('fs');
const fs = require('fs-extra');
const path = require('path');
const solc = require('solc');

//clean up
const compileDir = path.resolve(__dirname, '../compiled');
fs.removeSync(compileDir);
fs.ensureDirSync(compileDir);

//compile
const contractPath = path.resolve(__dirname, '../contracts','Car.sol');
const contractSource = fs.readFileSync(contractPath, 'utf8');

const result = solc.compile(contractSource,1);
console.log(result);

//check errors
if (Array.isArray(result.errors) && result.errors.length > 0) {
    throw new Error(result.errors[0]);
}

//save to disk
Object.keys(result.contracts).forEach(
    name => {
        const contractName = name.replace(/^:/,'');
        const filePath = path.resolve(__dirname, '../compiled', `${contractName}.json`);
        fs.outputJsonSync(filePath, result.contracts[name]);
        console.log(`save compiled contract ${contractName} to ${filePath}`);
    }
);

